# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  # Be sure to include AuthenticationSystem in Application Controller instead
  include AuthenticatedSystem

  before_filter :calculate_attendees

  helper :all # include all helpers, all the time

  # See ActionController::RequestForgeryProtection for details
  # Uncomment the :secret if you're not using the cookie session store
  protect_from_forgery #:secret => 'e2889052f0d0ddf170e3ab0ee7b85462'

  attr_accessor :page

  def check_admin
  	unless authorized?
  		redirect_to eggs_path
  	end
  end

  def check_logged
  	 unless logged_in?
  		flash[:notice] = "Por favor faça login para continuar."
  		redirect_to login_path
  	end
  end

  def check_form
	if Attendee.find_by_user_login(current_user.login).has_form?
		flash[:notice] = "Você já respondeu ao questionário"
		redirect_to root_url
	end
  end


  def check_attendee
    unless logged_in? and current_user.has_attendee?
      flash[:notice] = "Por favor preencha o formulária de inscrição para continuar."
      redirect_to attendees_path
    end
  end

  def calculate_attendees
  	@max_lodge_size = Attendee.max_lodge_size
  	@n_lodge_attendees = Attendee.lodge_total
  	@n_attendees = Attendee.total
  end

  # See ActionController::Base for details
  # Uncomment this to filter the contents of submitted sensitive data parameters
  # from your application log (in this case, all fields with names like "password").
  # filter_parameter_logging :password
end
